In [1]:
3+5


Out[1]:
8

In [2]:
3*6


Out[2]:
18

In [5]:
7 // 4


Out[5]:
1

In [6]:
x = 5

In [7]:
x*6


Out[7]:
30

In [8]:
y = x

In [9]:
x = x*2

In [21]:
m = 12
v = 3.8

E = 1/2*m*v**2
print('Energy {}*{}^2/2 = {}  '.format(m, v, E))


Energy 12*3.8^2/2 = 86.64  

In [26]:
import math

math.sqrt(2)
math.pow(3, 2.3)


Out[26]:
12.513502532843182

In [30]:
print(1, math.sqrt(1))
print(2, math.sqrt(2))
print(3, math.sqrt(3))
print(4, math.sqrt(4))


1 1.0
2 1.4142135623730951
3 1.7320508075688772
4 2.0

Computes $y = \sqrt{x}$


In [34]:
# My comment
for i in range(1,11):
    print(i, math.sqrt(i))


1 1.0
2 1.4142135623730951
3 1.7320508075688772
4 2.0
5 2.23606797749979
6 2.449489742783178
7 2.6457513110645907
8 2.8284271247461903
9 3.0
10 3.1622776601683795

In [1]:
for i in range(1,11):
    for j in range(1,11):
        print(i*j, end=' ')
    print()


1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 
6 12 18 24 30 36 42 48 54 60 
7 14 21 28 35 42 49 56 63 70 
8 16 24 32 40 48 56 64 72 80 
9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 100 

In [47]:
for i in range(2,41):
    print(i/2, (i/2)**2, (i/2)**3, end="")


  File "<ipython-input-47-24e5a3a45e12>", line 2
    print(i/2, (i/2)**2, (i/2)**3, end="")
        ^
IndentationError: expected an indented block

In [54]:
x_2 = 0
x_1 = 1
print(x_1, end="\n")
for i in range(3):
    y = x_1 + x_2
    print(x_2, x_1, y, end="\n")
    x_2 = x_1
    x_1 = y


1
0 1 1
1 1 2
1 2 3

In [5]:
import math

r = 0.24
T = 13
S0 = 100
for i in range(T):
    print(i, S0*math.exp(r*i/12))


0 100.0
1 102.02013400267558
2 104.08107741923882
3 106.18365465453596
4 108.32870676749586
5 110.51709180756477
6 112.74968515793758
7 115.02737988572274
8 117.35108709918103
9 119.72173631218101
10 122.14027581601698
11 124.60767305873807
12 127.12491503214048

In [8]:
import matplotlib.pylab as plt

r = 0.24
T = 130
S0 = 100
S = [S0*math.exp(r*i/12) for i in range(T)]

plt.plot(range(T), S, 'o-')
plt.xlabel('month')
plt.show()



In [12]:
plt.plot([1,2,5],'ro-')
plt.show()



In [13]:
[2*i for i in range(6)]


Out[13]:
[0, 2, 4, 6, 8, 10]

In [14]:
[i*i for i in [j*j for j in [0,1,2]]]


Out[14]:
[0, 1, 16]

In [16]:
[[j for j in range(i)] for i in range(5)]


Out[16]:
[[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

In [17]:
[[j,j**2,j**3] for j in range(10)]


Out[17]:
[[0, 0, 0],
 [1, 1, 1],
 [2, 4, 8],
 [3, 9, 27],
 [4, 16, 64],
 [5, 25, 125],
 [6, 36, 216],
 [7, 49, 343],
 [8, 64, 512],
 [9, 81, 729]]

$\pi\alpha \frac{3}{\eta}$


In [25]:
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.unicode'] = True

TH = [th/360*2*math.pi  for th in range(-360,361)]
s = [math.sin(th/360*2*math.pi)  for th in range(-360,361)]
c = [math.cos(th/360*2*math.pi)  for th in range(-360,361)]

plt.plot(TH, s, 'r')
plt.plot(TH, c, 'b')
plt.xticks([-2*math.pi, 0, 2*math.pi],[r'$2\pi$','$0$','$2\pi$'])
plt.show()



In [38]:
import numpy as np

[np.random.randn() for i in range(100)]


Out[38]:
[-0.011807548202017385,
 -1.9461209461814122,
 -0.19915561901992593,
 0.014018501616641324,
 -1.1135523666417193,
 0.5650925003202028,
 -1.3649613232322169,
 -0.8578819286343073,
 0.29608270427347755,
 -1.7431289548048878,
 0.5740837571534304,
 0.028971275503692513,
 -0.9214042180742472,
 0.3803763882434338,
 -0.8432556874760836,
 -0.6556679673540551,
 -0.7452150931854018,
 -1.313188673536765,
 -1.007198536949276,
 -0.6784674760177831,
 0.78621532098595,
 0.4758270222516398,
 0.9290871126086258,
 0.406731610597391,
 -0.21300443992543538,
 -0.09268908348848165,
 -0.6136902203900447,
 -0.0249986371533532,
 1.8254440925237343,
 -0.2708690970348345,
 -0.9937868918668932,
 -0.20974192447458403,
 -0.10361862030665692,
 2.053606415294265,
 -0.7801719352143298,
 -2.5125096136471967,
 -1.3934962829375763,
 0.1730272370835621,
 -0.5541528280663021,
 0.8544210011930802,
 -0.5180083314765113,
 0.8697357328782106,
 1.4475802287813024,
 -0.7144457225790304,
 1.0349380104762815,
 0.8996132100094439,
 0.32150879603723753,
 -0.15759991759080547,
 0.05105606165886863,
 1.035991435357906,
 -1.006061142215128,
 -0.12222671787454199,
 0.26811817612022054,
 0.3045796576148536,
 -0.5791198067024285,
 -0.8802961589120581,
 -0.31220601908548207,
 -0.3187368746642745,
 -0.47947629174766554,
 0.17391059649362578,
 0.7603939079135105,
 1.1626530097837955,
 1.3524290035612094,
 -0.5704790806832066,
 -1.952270750970555,
 -0.6967792895694571,
 1.3490088168436438,
 -0.3359986029314487,
 0.44787859748209424,
 0.08457355078510183,
 1.6032845072125474,
 0.6484154527415774,
 0.668075901743347,
 0.22521644178701597,
 -0.6505745823612619,
 0.09710022714363602,
 -0.866912919003709,
 -0.167808112972553,
 0.38188780040363424,
 -0.5659157768975556,
 -1.2574278903881668,
 0.8389978689387926,
 0.03826191716348811,
 0.20769534371865292,
 -1.0530318342990455,
 -0.4906013381964353,
 -1.60587441762734,
 2.0072190764673277,
 1.6546784864046558,
 0.8024202875964558,
 1.5323004575812509,
 -0.31416056823691424,
 0.20175049023577982,
 1.9006837950156665,
 -0.9152838267521616,
 0.20982887457462102,
 -1.053114177700731,
 0.11880205498438184,
 0.17768251229484647,
 0.08017383533250486]

In [2]:
import numpy as np
import matplotlib.pylab as plt

plt.plot(np.random.randn(100))
plt.show()



In [5]:
x = np.random.randn(10000)
plt.hist(x, bins=200)
plt.show()



In [8]:
x = [2,3,7,9]

sum = 0.
for i in range(len(x)):
    sum+= x[i]

print(sum/len(x))


5.25

In [1]:
Sum = 0.
for e in x:
    Sum += e

print(Sum/len(x))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-414e5e2baaa8> in <module>()
      1 Sum = 0.
----> 2 for e in x:
      3     Sum+= e
      4 
      5 print(Sum/len(x))

NameError: name 'x' is not defined

In [10]:
sig = 0.5
N = 100
L = 10
e = [sig*np.random.randn() for i in range(N)]

x = [0]*N
for i in range(N-1):
    x[i+1] = x[i] + e[i]

y = [0]*N

for i in range(N):
    y[i] = np.mean(x[max(i-L,0):(i+1)])
    

plt.plot(x)

plt.plot(y, 'r')
plt.show()


Algorithmic trade


In [46]:
%matplotlib inline

plt.figure(figsize=(12,5))

L = 30  # Moving average window
N = 500 # Number of timesteps

# Generate Gaussian noise
e = np.random.randn(N)

# Brownian walk
y = np.zeros_like(e)

y[0] = e[0]
for t in range(1,N):
    y[t] = y[t-1] + e[t]

mav = np.zeros_like(e)

for t in range(1,N):
    idx0 = max(0, t-L)
    mav[t] = np.sum(y[idx0:t])/L
    
    #print(len(y[idx0:t]))

buy = []
sell = []

for t in range(1,N):
    if y[t-1]<mav[t-1] and y[t]>mav[t]:
        buy.append(t)
    
    if y[t-1]>mav[t-1] and y[t]<mav[t]:
        sell.append(t)
    

plt.plot(y)
plt.plot(mav)
plt.plot(sell, mav[sell],'vr')
plt.plot(buy,mav[buy], '^b')
plt.show()



In [28]:
e[1:8]


Out[28]:
array([ 0.69180989, -1.16526685, -0.79924475, -0.69122179, -1.57623787,
       -0.77022945,  0.87183719])

In [42]:
v = [0,1,2,3,4,5]

v[-1:-6:-2]


Out[42]:
[5, 3, 1]

In [112]:
a = 5
b = 6
mu = 1
sig = 3

N = 100000

z = mu + sig*np.random.randn(N)

#plt.hist(z, bins=50)
#plt.show()

count = 0
for i in range(N):
    if z[i]>=a and z[i]<=b:
        count+=1

print(count/N)


0.04329

In [113]:
import numpy as np

np.log(5)


Out[113]:
1.6094379124341003

European


In [164]:
S0 = 100
r = 0.1
T = 1
sigma = 0.5
K = 120

Num = 10000

opt = 'Put'
#opt = 'Call'

C_T = 0.0
for i in range(Num):
    S_T = S0*np.exp(T*(r - 0.5*sigma**2) + sigma*np.sqrt(T)*np.random.randn())
    if opt=='Call':
        C_T += np.max([S_T-K,0])
    else:
        C_T += np.max([K-S_T,0])
        
        
C_T = C_T/Num

print('{}:'.format(opt), np.exp(-r*T)*C_T)


Put: 25.3531139008

Asian


In [151]:
S0 = 100
r = 0.1
T = 1
sigma = 0.05
K = 90
N = 100

Num = 10000

C_T = 0.0
for i in range(Num):
    S = [0]*N
    S[0] = S0
    for n in range(1,N):
        S[n] = S[n-1]*np.exp(T/N*(r - 0.5*sigma**2) + sigma*np.sqrt(T/N)*np.random.randn())
    
    C_T += np.max([np.mean(S)-K,0])

C_T = C_T/Num
print('Asian Call:', np.exp(-r*T)*C_T)


Asian Call: 13.6679815687

Lookback


In [153]:
S0 = 100
r = 0.1
T = 1
sigma = 0.05
K = 90
N = 10

Num = 10000

C_T = 0.0
for i in range(Num):
    S = [0]*N
    S[0] = S0
    for n in range(1,N):
        S[n] = S[n-1]*np.exp(T/N*(r - 0.5*sigma**2) + sigma*np.sqrt(T/N)*np.random.randn())
    
    C_T += np.max([np.max(S)-K,0])

C_T = C_T/Num
print('Lookback Call:', np.exp(-r*T)*C_T)


Lookback Call: 18.0746854795

Floating Lookback


In [154]:
S0 = 100
r = 0.1
T = 1
sigma = 0.05
K = 90
N = 10

Num = 10000

C_T = 0.0
for i in range(Num):
    S = [0]*N
    S[0] = S0
    for n in range(1,N):
        S[n] = S[n-1]*np.exp(T/N*(r - 0.5*sigma**2) + sigma*np.sqrt(T/N)*np.random.randn())
    
    C_T += np.max([S[-1]-np.min(S),0])

C_T = C_T/Num
print('Floating Lookback Call:', np.exp(-r*T)*C_T)


Floating Lookback Call: 9.03133666784

In [166]:
x = -3.7

if x<0:
    z = -1
else:
    z = 1
    
z = -1 if x<0 else 1

In [167]:
z


Out[167]:
-1

In [169]:
[i*2 for i in range(5)]

i = 0
while i<10:
    i+=2
    print(i)


2
4
6
8
10

In [171]:
import cmath

cmath.phase(1+2j)


Out[171]:
1.1071487177940904

In [172]:
1+3j + 2-5j


Out[172]:
(3-2j)

In [202]:
L = [['c',1], ['z',2], ['a',2]]

L.sort(key=lambda x: x[1])
print(L)
#L.sort(key=lambda x: x[0])
#print(L)


[['c', 1], ['z', 2], ['a', 2]]

In [184]:
L


Out[184]:
[['c', 1], ['a', 2]]

In [204]:
f = open('grades.txt')
N = int(f.readline())

L = []
for i in range(N):
    name = str(f.readline()).rstrip()
    grade = float(f.readline())
    #print(name, grade)
    L.append([name, grade])

L.sort(key=lambda x:x[0])    
L.sort(key=lambda x:x[1])
#print(L)

if len(L)<1:
    print('')
else:
    i = 0
    for z in L:
        if i==0:
            mn = z[1]

        if mn<z[1]:
            break
        else:
            i+=1

    temp = L[i][1]
    for j in range(i, len(L)):
        if temp<L[j][1]:
            break
        else:
            print(L[j][0])

 
    
f.close()


Ali
Ayse
Zeynep

In [205]:
L


Out[205]:
[['Fatma', 3.0],
 ['Veli', 3.0],
 ['Ali', 4.0],
 ['Ayse', 4.0],
 ['Zeynep', 4.0],
 ['Mehmet', 7.0]]

In [ ]:
USD 5.48
GBP 6.99
EUR 6.22

In [2]:
import pandas as pd

In [4]:
aapl = pd.read_csv("aapl.csv", index_col=0, parse_dates=True)

In [5]:
aapl


Out[5]:
Open High Low Close Volume Adj Close
Date
2012-01-03 409.399998 412.499989 408.999989 411.230000 75555200 53.784500
2012-01-04 410.000011 414.680012 409.279995 413.440010 65005500 54.073546
2012-01-05 414.950005 418.550007 412.670006 418.029995 67817400 54.673867
2012-01-06 419.770008 422.749996 419.220009 422.400002 79573200 55.245417
2012-01-09 425.499992 427.750004 421.349991 421.730000 98506100 55.157788
2012-01-10 425.909988 426.000004 421.500008 423.239994 64549100 55.355279
2012-01-11 422.680008 422.849998 419.309998 422.549992 53771200 55.265034
2012-01-12 422.279999 422.900013 418.750011 421.389992 53146800 55.113319
2012-01-13 419.699993 420.449997 418.659996 419.810009 56505400 54.906674
2012-01-17 424.199989 425.989990 422.959988 424.700001 60724300 55.546233
2012-01-18 426.959999 429.469990 426.300011 429.110008 69197800 56.123015
2012-01-19 430.150005 431.370007 426.510002 427.750004 65434600 55.945140
2012-01-20 427.489998 427.500011 419.750008 420.300007 103493600 54.970760
2012-01-23 422.669994 428.449993 422.299999 427.409996 76515600 55.900671
2012-01-24 425.100010 425.100010 419.550003 420.409996 136909500 54.985146
2012-01-25 454.439987 454.449974 443.729996 446.659996 239578500 58.418366
2012-01-26 448.360008 448.789978 443.139996 444.629990 80996300 58.152863
2012-01-27 444.339996 448.480011 443.769997 447.280010 74927300 58.499457
2012-01-30 445.709988 453.900002 445.390007 453.009995 94835300 59.248878
2012-01-31 455.589973 458.240021 453.070023 456.479980 97920900 59.702715
2012-02-01 458.410011 458.989998 455.550026 456.189987 67511500 59.664787
2012-02-02 455.899994 457.169983 453.980003 455.120003 46699100 59.524845
2012-02-03 457.300026 460.000008 455.560013 459.680000 71649900 60.121244
2012-02-06 458.379997 464.979988 458.200020 463.969978 62353200 60.682327
2012-02-07 465.250008 469.749977 464.579979 468.830009 79055900 61.317967
2012-02-08 470.500008 476.789978 469.699989 476.680016 101972500 62.344664
2012-02-09 480.760002 496.750008 480.559998 493.170006 221053700 64.501379
2012-02-10 490.960022 497.619987 488.549980 493.419998 157825500 64.534076
2012-02-13 499.529991 503.830009 497.089989 502.600021 129304000 65.734725
2012-02-14 504.659988 509.560020 502.000008 509.459991 115099600 66.631936
... ... ... ... ... ... ...
2012-11-15 537.530006 539.499985 522.620026 525.619987 197477700 69.355985
2012-11-16 525.200005 530.000008 505.750000 527.680008 316723400 69.627806
2012-11-19 540.709999 567.499985 539.880020 565.730011 205829400 74.648535
2012-11-20 571.910019 571.950020 554.580009 560.909981 160688500 74.012528
2012-11-21 564.249977 567.369995 556.599976 561.700012 93250500 74.116773
2012-11-23 567.169991 572.000008 562.600006 571.500023 68206600 75.409892
2012-11-26 575.900017 589.999992 573.710007 589.530022 157644900 77.788965
2012-11-27 589.549995 590.419975 580.100006 584.780006 133332500 77.162197
2012-11-28 577.269981 585.800003 572.259987 582.940018 130216100 76.919409
2012-11-29 590.220024 594.250023 585.249977 589.359978 128674700 77.766528
2012-11-30 586.789986 588.400009 582.679985 585.279991 97829900 77.228170
2012-12-03 593.650009 594.590004 585.500023 586.190025 91070000 77.348250
2012-12-04 581.800018 581.800018 572.129997 575.849976 139267100 75.983872
2012-12-05 568.910004 569.249985 538.769981 538.790009 261159500 71.093779
2012-12-06 528.940010 553.310020 518.629974 547.239975 294303100 72.208759
2012-12-07 553.400009 555.199997 530.000008 533.250015 196760200 70.362773
2012-12-10 525.000000 538.510002 521.580002 529.819977 157621100 69.910177
2012-12-11 539.770004 549.559975 537.370003 541.390015 148086400 71.436852
2012-12-12 547.769974 547.999992 536.270004 539.000000 121786000 71.121488
2012-12-13 531.149994 537.640022 525.800018 529.689987 156314900 69.893024
2012-12-14 514.749992 518.129990 505.580009 509.789986 252394800 67.267203
2012-12-17 508.929993 519.999992 501.230003 518.829979 189401800 68.460037
2012-12-18 525.000000 534.899986 520.249985 533.900017 156421300 70.448541
2012-12-19 531.470001 533.700012 525.499985 526.309990 112342300 69.447031
2012-12-20 530.000008 530.200012 518.880020 521.730019 120422400 68.842700
2012-12-21 512.469994 519.669998 510.239983 519.330017 149067100 68.526018
2012-12-24 520.350014 524.250023 518.709976 520.169983 43938300 68.636852
2012-12-26 519.000023 519.460007 511.120003 512.999992 75609100 67.690766
2012-12-27 513.539978 516.250000 504.659988 515.060013 113780100 67.962587
2012-12-28 510.290024 514.480026 508.119987 509.589981 88569600 67.240812

249 rows × 6 columns


In [9]:
import pandas as pd
import pandas_datareader as web

import datetime

start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2018, 11, 5)
msft = web.DataReader("MSFT", 'yahoo', start, end)
aapl = web.DataReader("AAPL", 'yahoo', start, end)

In [10]:
intl = web.DataReader("INTL", 'yahoo', start, end)

In [12]:
bist = web.DataReader("XU100.IS", 'yahoo', start, end)

In [13]:
bist


Out[13]:
Open High Low Close Adj Close Volume
Date
2015-01-06 86909.601563 86909.601563 86909.601563 86909.601563 86909.601563 0.0
2015-01-07 86776.601563 86776.601563 86776.601563 86776.601563 86776.601563 0.0
2015-01-08 87689.601563 87689.601563 87689.601563 87689.601563 87689.601563 0.0
2015-01-09 87648.398438 87648.398438 87648.398438 87648.398438 87648.398438 0.0
2015-01-12 NaN NaN NaN NaN NaN NaN
2015-01-13 88313.500000 88313.500000 88313.500000 88313.500000 88313.500000 0.0
2015-01-14 87563.500000 87563.500000 87563.500000 87563.500000 87563.500000 0.0
2015-01-15 87742.796875 87742.796875 87742.796875 87742.796875 87742.796875 0.0
2015-01-16 NaN NaN NaN NaN NaN NaN
2015-01-19 88006.500000 88006.500000 88006.500000 88006.500000 88006.500000 0.0
2015-01-20 NaN NaN NaN NaN NaN NaN
2015-01-21 90312.796875 90312.796875 90312.796875 90312.796875 90312.796875 0.0
2015-01-22 90902.796875 90902.796875 90902.796875 90902.796875 90902.796875 0.0
2015-01-23 90736.000000 90736.000000 90736.000000 90736.000000 90736.000000 0.0
2015-01-26 91412.898438 91412.898438 91412.898438 91412.898438 91412.898438 0.0
2015-01-27 91150.000000 91150.000000 91150.000000 91150.000000 91150.000000 0.0
2015-01-28 NaN NaN NaN NaN NaN NaN
2015-01-29 88557.796875 88557.796875 88557.796875 88557.796875 88557.796875 0.0
2015-01-30 88945.796875 88945.796875 88945.796875 88945.796875 88945.796875 0.0
2015-02-02 89712.796875 89712.796875 89712.796875 89712.796875 89712.796875 0.0
2015-02-03 NaN NaN NaN NaN NaN NaN
2015-02-04 85910.101563 85910.101563 85910.101563 85910.101563 85910.101563 0.0
2015-02-05 85789.296875 85789.296875 85789.296875 85789.296875 85789.296875 0.0
2015-02-06 84987.398438 84987.398438 84987.398438 84987.398438 84987.398438 0.0
2015-02-09 84574.296875 84574.296875 84574.296875 84574.296875 84574.296875 0.0
2015-02-10 83116.398438 83116.398438 83116.398438 83116.398438 83116.398438 0.0
2015-02-11 83429.500000 83429.500000 83429.500000 83429.500000 83429.500000 0.0
2015-02-12 86082.500000 86082.500000 86082.500000 86082.500000 86082.500000 0.0
2015-02-13 85809.203125 85809.203125 85809.203125 85809.203125 85809.203125 0.0
2015-02-16 86040.500000 86040.500000 86040.500000 86040.500000 86040.500000 0.0
... ... ... ... ... ... ...
2018-01-01 NaN NaN NaN NaN NaN NaN
2018-01-02 117524.000000 117524.000000 117524.000000 117524.000000 117524.000000 0.0
2018-01-03 116052.000000 116052.000000 116052.000000 116052.000000 116052.000000 0.0
2018-01-04 115491.000000 115491.000000 115491.000000 115491.000000 115491.000000 0.0
2018-01-05 116638.000000 116638.000000 116638.000000 116638.000000 116638.000000 0.0
2018-01-08 116638.000000 116638.000000 116638.000000 116638.000000 116638.000000 0.0
2018-01-09 115023.000000 115023.000000 115023.000000 115023.000000 115023.000000 0.0
2018-01-10 113589.000000 113589.000000 113589.000000 113589.000000 113589.000000 0.0
2018-01-11 114719.000000 114719.000000 114719.000000 114719.000000 114719.000000 0.0
2018-01-12 114645.000000 114645.000000 114645.000000 114645.000000 114645.000000 0.0
2018-01-15 112159.000000 112159.000000 112159.000000 112159.000000 112159.000000 0.0
2018-01-16 114418.000000 114418.000000 114418.000000 114418.000000 114418.000000 0.0
2018-01-17 116593.000000 116593.000000 116593.000000 116593.000000 116593.000000 0.0
2018-01-18 116856.000000 116856.000000 116856.000000 116856.000000 116856.000000 0.0
2018-01-19 115147.000000 115147.000000 115147.000000 115147.000000 115147.000000 0.0
2018-01-22 117235.000000 117235.000000 117235.000000 117235.000000 117235.000000 0.0
2018-01-23 118400.000000 118400.000000 118400.000000 118400.000000 118400.000000 0.0
2018-01-24 119648.000000 119648.000000 119648.000000 119648.000000 119648.000000 0.0
2018-01-25 118604.000000 118604.000000 118604.000000 118604.000000 118604.000000 0.0
2018-01-26 120702.000000 120702.000000 120702.000000 120702.000000 120702.000000 0.0
2018-01-29 120845.000000 120845.000000 120845.000000 120845.000000 120845.000000 0.0
2018-01-30 119303.000000 119303.000000 119303.000000 119303.000000 119303.000000 0.0
2018-01-31 119529.000000 119529.000000 119529.000000 119529.000000 119529.000000 0.0
2018-02-01 119044.000000 119044.000000 119044.000000 119044.000000 119044.000000 0.0
2018-02-02 118119.000000 118119.000000 118119.000000 118119.000000 118119.000000 0.0
2018-02-05 116853.000000 116853.000000 116853.000000 116853.000000 116853.000000 0.0
2018-02-06 115457.000000 115457.000000 115457.000000 115457.000000 115457.000000 0.0
2018-02-07 115570.000000 115570.000000 115570.000000 115570.000000 115570.000000 0.0
2018-02-08 114264.000000 114264.000000 114264.000000 114264.000000 114264.000000 0.0
2018-02-09 113590.000000 113590.000000 113590.000000 113590.000000 113590.000000 0.0

809 rows × 6 columns


In [11]:
intl


Out[11]:
Open High Low Close Adj Close Volume
Date
2014-12-31 20.650000 20.879999 20.410000 20.570000 20.570000 54400
2015-01-02 20.639999 20.910000 19.990000 20.230000 20.230000 67200
2015-01-05 20.250000 20.650000 20.209999 20.490000 20.490000 96100
2015-01-06 20.700001 20.879999 20.250000 20.510000 20.510000 169700
2015-01-07 20.760000 20.799999 20.379999 20.540001 20.540001 71200
2015-01-08 20.469999 20.790001 20.230000 20.410000 20.410000 91900
2015-01-09 20.370001 20.389999 19.969999 20.040001 20.040001 37600
2015-01-12 19.740000 20.000000 19.230000 19.830000 19.830000 98300
2015-01-13 19.840000 20.500000 19.690001 19.959999 19.959999 74000
2015-01-14 19.809999 19.969999 19.570000 19.879999 19.879999 36500
2015-01-15 19.959999 20.080000 19.500000 19.770000 19.770000 44000
2015-01-16 19.510000 19.910000 19.010000 19.260000 19.260000 124000
2015-01-20 19.160000 19.500000 18.730000 19.250000 19.250000 95300
2015-01-21 19.139999 19.520000 19.139999 19.440001 19.440001 30600
2015-01-22 19.540001 19.580000 19.070000 19.440001 19.440001 46700
2015-01-23 19.440001 19.500000 19.330000 19.440001 19.440001 19100
2015-01-26 19.290001 19.469999 19.160000 19.440001 19.440001 41700
2015-01-27 19.250000 19.480000 19.160000 19.410000 19.410000 33500
2015-01-28 19.530001 19.590000 19.320000 19.400000 19.400000 92000
2015-01-29 19.440001 19.600000 19.219999 19.600000 19.600000 33100
2015-01-30 19.389999 19.750000 19.260000 19.280001 19.280001 55900
2015-02-02 19.440001 19.840000 19.139999 19.760000 19.760000 54200
2015-02-03 19.450001 20.250000 19.450001 20.070000 20.070000 93500
2015-02-04 20.070000 20.250000 19.840000 19.990000 19.990000 74300
2015-02-05 20.000000 20.120001 19.730000 19.969999 19.969999 61100
2015-02-06 20.170000 20.170000 19.840000 19.900000 19.900000 66900
2015-02-09 19.850000 20.100000 19.740000 19.879999 19.879999 55100
2015-02-10 20.250000 20.600000 19.910000 20.530001 20.530001 348700
2015-02-11 20.600000 20.900000 20.600000 20.770000 20.770000 135200
2015-02-12 20.860001 23.000000 20.830000 22.969999 22.969999 243300
... ... ... ... ... ... ...
2018-09-25 48.910000 49.590000 48.259998 49.060001 49.060001 111700
2018-09-26 49.049999 49.340000 48.529999 48.830002 48.830002 82800
2018-09-27 48.790001 49.029999 48.389999 48.410000 48.410000 60000
2018-09-28 48.270000 48.709999 48.060001 48.320000 48.320000 57900
2018-10-01 48.480000 49.279999 48.279999 48.509998 48.509998 98700
2018-10-02 48.369999 48.580002 47.630001 47.680000 47.680000 64700
2018-10-03 47.779999 49.090000 47.770000 48.799999 48.799999 105900
2018-10-04 47.990002 49.119999 47.990002 48.619999 48.619999 67800
2018-10-05 48.619999 49.110001 47.570000 48.000000 48.000000 57600
2018-10-08 48.000000 48.509998 46.580002 46.580002 46.580002 72900
2018-10-09 46.369999 46.730000 46.009998 46.459999 46.459999 59000
2018-10-10 46.439999 46.639999 45.200001 45.959999 45.959999 114200
2018-10-11 45.509998 46.919998 45.509998 45.650002 45.650002 138300
2018-10-12 45.970001 46.290001 44.790001 44.790001 44.790001 95000
2018-10-15 44.779999 45.570000 44.070000 44.889999 44.889999 62900
2018-10-16 45.070000 46.599998 44.860001 46.520000 46.520000 48900
2018-10-17 46.389999 46.980000 45.349998 46.849998 46.849998 91100
2018-10-18 46.639999 47.049999 45.779999 46.160000 46.160000 43700
2018-10-19 46.080002 46.500000 44.299999 45.480000 45.480000 63700
2018-10-22 45.669998 46.180000 45.279999 45.509998 45.509998 50200
2018-10-23 45.020000 45.150002 43.889999 44.110001 44.110001 69300
2018-10-24 44.040001 44.560001 42.000000 42.070000 42.070000 72300
2018-10-25 42.230000 44.169998 42.230000 43.759998 43.759998 92400
2018-10-26 43.110001 44.580002 43.000000 44.240002 44.240002 75200
2018-10-29 44.689999 45.590000 44.419998 45.299999 45.299999 64300
2018-10-30 45.360001 46.860001 45.360001 46.570000 46.570000 45800
2018-10-31 47.029999 47.889999 45.169998 45.279999 45.279999 51600
2018-11-01 45.320000 47.560001 45.310001 46.090000 46.090000 46900
2018-11-02 46.320000 47.380001 46.320000 47.220001 47.220001 66400
2018-11-05 47.230000 47.470001 46.759998 47.240002 47.240002 57300

970 rows × 6 columns


In [8]:
aapl


Out[8]:
Open High Low Close Adj Close Volume
Date
2014-12-31 112.820000 113.129997 110.209999 110.379997 103.302063 41403400
2015-01-02 111.389999 111.440002 107.349998 109.330002 102.319397 53204600
2015-01-05 108.290001 108.650002 105.410004 106.250000 99.436890 64285500
2015-01-06 106.540001 107.430000 104.629997 106.260002 99.446251 65797100
2015-01-07 107.199997 108.199997 106.699997 107.750000 100.840714 40105900
2015-01-08 109.230003 112.150002 108.699997 111.889999 104.715256 59364500
2015-01-09 112.669998 113.250000 110.209999 112.010002 104.827553 53699500
2015-01-12 112.599998 112.629997 108.800003 109.250000 102.244522 49650800
2015-01-13 111.430000 112.800003 108.910004 110.220001 103.152313 67091900
2015-01-14 109.040001 110.489998 108.500000 109.800003 102.759262 48337000
2015-01-15 110.000000 110.059998 106.660004 106.820000 99.970337 60014000
2015-01-16 107.029999 107.580002 105.199997 105.989998 99.193558 78513300
2015-01-20 107.839996 108.970001 106.500000 108.720001 101.748512 49899900
2015-01-21 108.949997 111.059998 108.269997 109.550003 102.525291 48575900
2015-01-22 110.260002 112.470001 109.720001 112.400002 105.192535 53796400
2015-01-23 112.300003 113.750000 111.529999 112.980003 105.735344 46464800
2015-01-26 113.739998 114.360001 112.800003 113.099998 105.847649 55615000
2015-01-27 112.419998 112.480003 109.029999 109.139999 102.141586 95568700
2015-01-28 117.629997 118.120003 115.309998 115.309998 107.915932 146477100
2015-01-29 116.320000 119.190002 115.559998 118.900002 111.275742 84436400
2015-01-30 118.400002 120.000000 116.849998 117.160004 109.647324 83745500
2015-02-02 118.050003 119.169998 116.080002 118.629997 111.023064 62739100
2015-02-03 118.500000 119.089996 117.610001 118.650002 111.041771 51915700
2015-02-04 118.500000 120.510002 118.309998 119.559998 111.893402 70149700
2015-02-05 120.019997 120.230003 119.250000 119.940002 112.692047 42246200
2015-02-06 120.019997 120.250000 118.449997 118.930000 111.743088 43706600
2015-02-09 118.550003 119.839996 118.430000 119.720001 112.485352 38889800
2015-02-10 120.169998 122.150002 120.160004 122.019997 114.646355 62008500
2015-02-11 122.769997 124.919998 122.500000 124.879997 117.333511 73561800
2015-02-12 126.059998 127.480003 125.570000 126.459999 118.818039 74474500
... ... ... ... ... ... ...
2018-09-25 219.750000 222.820007 219.699997 222.190002 222.190002 24554400
2018-09-26 221.000000 223.750000 219.759995 220.419998 220.419998 23984700
2018-09-27 223.820007 226.440002 223.539993 224.949997 224.949997 30181200
2018-09-28 224.789993 225.839996 224.020004 225.740005 225.740005 22929400
2018-10-01 227.949997 229.419998 226.350006 227.259995 227.259995 23600800
2018-10-02 227.250000 230.000000 226.630005 229.279999 229.279999 24788200
2018-10-03 230.050003 233.470001 229.779999 232.070007 232.070007 28654800
2018-10-04 230.779999 232.350006 226.729996 227.990005 227.990005 32042000
2018-10-05 227.960007 228.410004 220.580002 224.289993 224.289993 33580500
2018-10-08 222.210007 224.800003 220.199997 223.770004 223.770004 29663900
2018-10-09 223.639999 227.270004 222.250000 226.869995 226.869995 26891000
2018-10-10 225.460007 226.350006 216.050003 216.360001 216.360001 41990600
2018-10-11 214.520004 219.500000 212.320007 214.449997 214.449997 53124400
2018-10-12 220.419998 222.880005 216.839996 222.110001 222.110001 40337900
2018-10-15 221.160004 221.830002 217.270004 217.360001 217.360001 30791000
2018-10-16 218.929993 222.990005 216.759995 222.149994 222.149994 29184000
2018-10-17 222.300003 222.639999 219.339996 221.190002 221.190002 22885400
2018-10-18 217.860001 219.740005 213.000000 216.020004 216.020004 32581300
2018-10-19 218.059998 221.259995 217.429993 219.309998 219.309998 33078700
2018-10-22 219.789993 223.360001 218.940002 220.649994 220.649994 28792100
2018-10-23 215.830002 223.250000 214.699997 222.729996 222.729996 38767800
2018-10-24 222.600006 224.229996 214.539993 215.089996 215.089996 40925500
2018-10-25 217.710007 221.380005 216.750000 219.800003 219.800003 29855800
2018-10-26 215.899994 220.190002 212.669998 216.300003 216.300003 47258400
2018-10-29 219.190002 219.690002 206.089996 212.240005 212.240005 45935500
2018-10-30 211.149994 215.179993 209.270004 213.300003 213.300003 36660000
2018-10-31 216.880005 220.449997 216.619995 218.860001 218.860001 38358900
2018-11-01 219.050003 222.360001 216.809998 222.220001 222.220001 58323200
2018-11-02 209.550003 213.649994 205.429993 207.479996 207.479996 91328700
2018-11-05 204.300003 204.389999 198.169998 201.589996 201.589996 66108400

970 rows × 6 columns


In [18]:
import matplotlib.pylab as plt


plt.figure(figsize=(12,3))
aapl['Close'].plot()
plt.show()



In [28]:
cols = ['Open', 'Close', 'Low', 'High', 'Volume']

for c in cols:
    plt.figure(figsize=(12,1))
    aapl['2015-08'][c].plot()
    plt.show()



In [25]:
aapl['2015-8-21':'2015-10-11']


Out[25]:
Open High Low Close Adj Close Volume
Date
2015-08-21 110.430000 111.900002 105.650002 105.760002 100.235687 128275500
2015-08-24 94.870003 108.800003 92.000000 103.120003 97.733597 162206300
2015-08-25 111.110001 111.110001 103.500000 103.739998 98.321205 103601600
2015-08-26 107.089996 109.889999 105.050003 109.690002 103.960411 96774600
2015-08-27 112.230003 113.239998 110.019997 112.919998 107.021690 84616100
2015-08-28 112.169998 113.309998 111.540001 113.290001 107.372368 53164400
2015-08-31 112.029999 114.529999 112.000000 112.760002 106.870049 56229300
2015-09-01 110.150002 111.879997 107.360001 107.720001 102.093315 76845900
2015-09-02 110.230003 112.339996 109.129997 112.339996 106.471985 61888800
2015-09-03 112.489998 112.779999 110.040001 110.370003 104.604881 53233900
2015-09-04 108.970001 110.449997 108.510002 109.269997 103.562332 49996300
2015-09-08 111.750000 112.559998 110.320000 112.309998 106.443550 54843600
2015-09-09 113.760002 114.019997 109.769997 110.150002 104.396370 85010800
2015-09-10 110.269997 113.279999 109.900002 112.570000 106.689964 62892800
2015-09-11 111.790001 114.209999 111.760002 114.209999 108.244316 49915500
2015-09-14 116.580002 116.889999 114.860001 115.309998 109.286827 58363400
2015-09-15 115.930000 116.529999 114.419998 116.279999 110.206169 43341200
2015-09-16 116.250000 116.540001 115.440002 116.410004 110.329376 37173500
2015-09-17 115.660004 116.489998 113.720001 113.919998 107.969444 64112600
2015-09-18 112.209999 114.300003 111.870003 113.449997 107.524002 74285300
2015-09-21 113.669998 115.370003 113.660004 115.209999 109.192062 50222000
2015-09-22 113.379997 114.180000 112.519997 113.400002 107.476608 50346200
2015-09-23 113.629997 114.720001 113.300003 114.320000 108.348557 35756700
2015-09-24 113.250000 115.500000 112.370003 115.000000 108.993042 50219500
2015-09-25 116.440002 116.690002 114.019997 114.709999 108.718178 56151900
2015-09-28 113.849998 114.570000 112.440002 112.440002 106.566750 52109000
2015-09-29 112.830002 113.510002 107.860001 109.059998 103.363312 73365400
2015-09-30 110.169998 111.540001 108.730003 110.300003 104.538551 66473000
2015-10-01 109.070000 109.620003 107.309998 109.580002 103.856148 63929100
2015-10-02 108.010002 111.010002 107.550003 110.379997 104.614357 58019800
2015-10-05 109.879997 111.370003 109.070000 110.779999 104.993469 52064700
2015-10-06 110.629997 111.739998 109.769997 111.309998 105.495781 48196800
2015-10-07 111.739998 111.769997 109.410004 110.779999 104.993469 46765600
2015-10-08 110.190002 110.190002 108.209999 109.500000 103.780327 61979600
2015-10-09 110.000000 112.279999 109.489998 112.120003 106.263466 52766100

In [29]:
dates = ['2015-08','2016-08','2017-08','2018-08']
for d in dates:
    plt.figure(figsize=(12,1))
    aapl[d]['Open'].plot()
    plt.show()



In [44]:
%matplotlib inline
aapl['2017-08'][['High','Low','Close']].plot();



In [48]:
aapl.plot(x='Volume', y='Close', kind='scatter')


Out[48]:
<matplotlib.axes._subplots.AxesSubplot at 0x1209d2c18>

In [53]:
df = pd.DataFrame({'a':[1,2,5],'b':[3, 9, 16], 'c':[10,11,12]},index=[1,2,3])

In [58]:
df.plot.bar();



In [74]:
df.plot.pie(subplots=True);



In [71]:
aapl.plot.hexbin(x='Volume',y='Open')


Out[71]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a22f6e198>

In [68]:
aapl['Open'].plot.hist(bins=200)
aapl['Open'].plot.density()


Out[68]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a22a16b00>

In [77]:
df[df.b % 2 == 0]


Out[77]:
a b c
3 5 16 12

In [82]:
aapl.iloc[1:100:2]


Out[82]:
Open High Low Close Adj Close Volume
Date
2015-01-02 111.389999 111.440002 107.349998 109.330002 102.319397 53204600
2015-01-06 106.540001 107.430000 104.629997 106.260002 99.446251 65797100
2015-01-08 109.230003 112.150002 108.699997 111.889999 104.715256 59364500
2015-01-12 112.599998 112.629997 108.800003 109.250000 102.244522 49650800
2015-01-14 109.040001 110.489998 108.500000 109.800003 102.759262 48337000
2015-01-16 107.029999 107.580002 105.199997 105.989998 99.193558 78513300
2015-01-21 108.949997 111.059998 108.269997 109.550003 102.525291 48575900
2015-01-23 112.300003 113.750000 111.529999 112.980003 105.735344 46464800
2015-01-27 112.419998 112.480003 109.029999 109.139999 102.141586 95568700
2015-01-29 116.320000 119.190002 115.559998 118.900002 111.275742 84436400
2015-02-02 118.050003 119.169998 116.080002 118.629997 111.023064 62739100
2015-02-04 118.500000 120.510002 118.309998 119.559998 111.893402 70149700
2015-02-06 120.019997 120.250000 118.449997 118.930000 111.743088 43706600
2015-02-10 120.169998 122.150002 120.160004 122.019997 114.646355 62008500
2015-02-12 126.059998 127.480003 125.570000 126.459999 118.818039 74474500
2015-02-17 127.489998 128.880005 126.919998 127.830002 120.105263 63152400
2015-02-19 128.479996 129.029999 128.330002 128.449997 120.687782 37362400
2015-02-23 130.020004 133.000000 129.660004 133.000000 124.962830 70974100
2015-02-25 131.559998 131.600006 128.149994 128.789993 121.007233 74711700
2015-02-27 130.000000 130.570007 128.240005 128.460007 120.697182 62014800
2015-03-03 128.960007 129.520004 128.089996 129.360001 121.542801 37816300
2015-03-05 128.580002 128.750000 125.760002 126.410004 118.771080 56517100
2015-03-09 127.959999 129.570007 125.059998 127.139999 119.456970 88528500
2015-03-11 124.750000 124.769997 122.110001 122.239998 114.853058 68939000
2015-03-13 124.400002 125.400002 122.580002 123.589996 116.121483 51827300
2015-03-17 125.900002 127.320000 125.650002 127.040001 119.362991 51023100
2015-03-19 128.750000 129.250000 127.400002 127.500000 119.795204 45809500
2015-03-23 127.120003 127.849998 126.519997 127.209999 119.522736 37709700
2015-03-25 126.540001 126.820000 123.379997 123.379997 115.924164 51655200
2015-03-27 124.570000 124.699997 122.910004 123.250000 115.802032 39546200
2015-03-31 126.089996 126.489998 124.360001 124.430000 116.910706 42090600
2015-04-02 125.029999 125.559998 124.190002 125.320000 117.746933 32220100
2015-04-07 127.639999 128.119995 125.980003 126.010002 118.395248 35012300
2015-04-09 125.849998 126.580002 124.660004 126.559998 118.912003 32484000
2015-04-13 128.369995 128.570007 126.610001 126.849998 119.184486 36365100
2015-04-15 126.410004 127.129997 126.010002 126.779999 119.118713 28970400
2015-04-17 125.550003 126.139999 124.459999 124.750000 117.211380 51957000
2015-04-21 128.100006 128.199997 126.669998 126.910004 119.240868 32435100
2015-04-23 128.300003 130.419998 128.139999 129.669998 121.834076 45770900
2015-04-27 132.309998 133.130005 131.149994 132.649994 124.633965 96954200
2015-04-29 130.160004 131.589996 128.300003 128.639999 120.866318 63386100
2015-05-01 126.099998 130.130005 125.300003 128.949997 121.157585 58512600
2015-05-05 128.149994 128.449997 125.779999 125.800003 118.197945 49271400
2015-05-07 124.769997 126.080002 124.019997 125.260002 118.182167 43940900
2015-05-11 127.389999 127.559998 125.629997 126.320000 119.182259 42035800
2015-05-13 126.150002 127.190002 125.870003 126.010002 118.889786 34694200
2015-05-15 129.070007 129.490005 128.210007 128.770004 121.493835 38208000
2015-05-19 130.690002 130.880005 129.639999 130.070007 122.720383 44633200
2015-05-21 130.070007 131.630005 129.830002 131.389999 123.965790 39730400
2015-05-26 132.600006 132.910004 129.119995 129.619995 122.295799 70697600

In [89]:
df.loc[:,'b':'c']


Out[89]:
b c
1 3 10
2 9 11
3 16 12

In [91]:
df2 = pd.DataFrame({'a':[7,-1,3,5],'b':[1, 1, 1,1], 'c':[10,11,12,18]},index=[7,9,15,18])

In [95]:
df3 = pd.concat([df, df2])

In [97]:
df3[df3.a>=0]


Out[97]:
a b c
1 1 3 10
2 2 9 11
3 5 16 12
7 7 1 10
15 3 1 12
18 5 1 18

In [104]:
aapl['Volume'][aapl.High - aapl.Low > 3].plot(style='.');



In [2]:
import math

def circle_area(radius):
    return radius**2*math.pi

circle_area(radius=1)


Out[2]:
3.141592653589793

In [3]:
def rect_area(height, width):
    return height*width

In [4]:
rect_area(width=3, height=2)


Out[4]:
6

Finding the root of a function


In [7]:
def f(x):
    return x**2 - 2

def sgn(x):
    return 1 if x>0 else -1

def find_root(a, b, epsilon=0.0000001):
    left = f(a)
    right = f(b)
    
    sgn_l = sgn(left)
    sgn_r = sgn(right)
    
    if sgn_l == sgn_r:
        error('No root in the interval')
    
    while (right-left>epsilon):
        mid = (right+left)/2
        f_mid = f(mid)
        sgn_mid = sgn(f_mid)
        if sgn_l==sgn_mid:
            left = mid
        else:
            right = mid
        
        print(left, right)
    
    return (right+left)/2


print(find_root(1,3))
print(math.sqrt(2))


-1 3.0
1.0 3.0
1.0 2.0
1.0 1.5
1.25 1.5
1.375 1.5
1.375 1.4375
1.40625 1.4375
1.40625 1.421875
1.4140625 1.421875
1.4140625 1.41796875
1.4140625 1.416015625
1.4140625 1.4150390625
1.4140625 1.41455078125
1.4140625 1.414306640625
1.4141845703125 1.414306640625
1.4141845703125 1.41424560546875
1.4141845703125 1.414215087890625
1.4141998291015625 1.414215087890625
1.4142074584960938 1.414215087890625
1.4142112731933594 1.414215087890625
1.4142131805419922 1.414215087890625
1.4142131805419922 1.4142141342163086
1.4142131805419922 1.4142136573791504
1.4142134189605713 1.4142136573791504
1.4142135381698608 1.4142136573791504
1.4142135381698608 1.4142135977745056
1.4142135679721832
1.4142135623730951

In [14]:
import matplotlib.pylab as plt
import numpy as np

def f(x):
    return x**2 - 2

def sgn(x):
    return 1 if x>0 else -1

def find_root(a, b, epsilon=0.0000001):
    left = a
    right = b
    
    A = [left]
    B = [right]
    
    sgn_l = sgn(left)
    sgn_r = sgn(right)
    
    if sgn_l == sgn_r:
        error('No root in the interval')
    
    while (right-left>epsilon):
        mid = (right+left)/2
        f_mid = f(mid)
        sgn_mid = sgn(f_mid)
        if sgn_l==sgn_mid:
            left = mid
            A.append(left)
        else:
            right = mid
            B.append(right)
        
        #print(left, right)
    
    return A, B

x = np.linspace(-3,3)
plt.plot(x, f(x), 'k-')

A, B = find_root(0, 3)
plt.plot(A, np.zeros_like(A), 'ro')
plt.plot(B, np.zeros_like(B), 'bo')

for a in A:
    plt.plot([a, a], [0, f(a)], ':r')
    
for b in B:
    plt.plot([b, b], [0, f(b)], ':b')

plt.show()



In [10]:
B


Out[10]:
[14,
 6.0,
 2.0,
 1.5,
 1.4375,
 1.421875,
 1.41796875,
 1.416015625,
 1.4150390625,
 1.41455078125,
 1.414306640625,
 1.41424560546875,
 1.414215087890625,
 1.4142141342163086,
 1.4142136573791504,
 1.4142135977745056]

In [8]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

In [3]:
x = [1,2,5,8,1,3,5]

for i in range(len(x)):
    x[i] = x[i]*3

x


Out[3]:
[3, 6, 15, 24, 3, 9, 15]

In [5]:
x = [z*3 for z in x]

In [9]:
x = np.array([1,2,5,8,1,3,5])
x*3


Out[9]:
array([ 3,  6, 15, 24,  3,  9, 15])

In [ ]:
1 2
2 5
5 8
8 1
1 3
3 5

In [12]:
for i in range(len(x)-1):
    for j in range(2):
        print(x[i+j], end=' ')
    print('')


1 2 
2 5 
5 8 
8 1 
1 3 
3 5 

1 2 2 5 5 8 8 1 1 3 3 5


In [20]:
N = 4
for i in range(len(x)-N+1):
    for j in range(N):
        print(x[i+N-1-j], end=' ')
    print('')


8 5 2 1 
1 8 5 2 
3 1 8 5 
5 3 1 8 

Binary Search


In [27]:
L = [1,3,4,7,8,9,12]

i = 0
j = len(L)-1
x = 12


found = False
while (i<=j):
    mid = (i+j)//2
    if L[mid]==x:
        found = True
        break
    elif L[mid]<x:
        i = mid+1
    elif L[mid]>x:
        j = mid-1

if found:
    print('Found')
else:
    print('Not Found')


Found